package cn.com.gridinfo.sooc.core.course.classes.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremy.arad.utils.AnimUtil;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.course.classes.activity.CourseListActivity;
import cn.com.gridinfo.sooc.core.course.classes.bean.KindsGrid;

/**
 * 课程分类 grid 适配器
 * Created by yingchunli on 2016/5/11.
 */
public class KindsGridAdapter extends BaseAdapter {

    private List<KindsGrid> kindsGridList;
    private Context context;

    public KindsGridAdapter(Context context, List<KindsGrid> cklist) {
        this.context = context;
        this.kindsGridList = cklist;
    }

    @Override
    public int getCount() {
        return kindsGridList.size();
    }

    @Override
    public Object getItem(int position) {
        return kindsGridList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.kinds_grid_item, parent, false);
        }
        //名称
        TextView titleLeft = (TextView) convertView.findViewById(R.id.grid_item_text);
        TextView titleRight = (TextView) convertView.findViewById(R.id.grid_item_text_right);

        titleLeft.setText(kindsGridList.get(position).getCourseKindsLeft().getXsm());
        titleRight.setText(kindsGridList.get(position).getCourseKindsRight().getXsm());

        //数目
        TextView countLeft = (TextView) convertView.findViewById(R.id.grid_item_count);
        TextView countRight = (TextView) convertView.findViewById(R.id.grid_item_count_right);

        countLeft.setText(kindsGridList.get(position).getCourseKindsLeft().getCount());
        countRight.setText(kindsGridList.get(position).getCourseKindsRight().getCount());

        RelativeLayout kinds_left = (RelativeLayout) convertView.findViewById(R.id.left_kind_item);
        RelativeLayout kinds_right = (RelativeLayout) convertView.findViewById(R.id.right_kind_item);

        if(kindsGridList.get(position).getCourseKindsLeft().getYxid()!=null) {
            kinds_left.setBackgroundResource(R.drawable.kinds_item_bg);
            kinds_left.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goNextActivity(kindsGridList.get(position).getCourseKindsLeft().getYxid(), kindsGridList.get(position).getCourseKindsLeft().getXsm());
                }
            });
        }else{
            kinds_left.setBackgroundResource(R.color.white);
        }
        if(kindsGridList.get(position).getCourseKindsRight().getYxid()!=null) {
            kinds_right.setBackgroundResource(R.drawable.kinds_item_bg);
            kinds_right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goNextActivity(kindsGridList.get(position).getCourseKindsRight().getYxid(), kindsGridList.get(position).getCourseKindsRight().getXsm());
                }
            });
        }else{
            kinds_right.setBackgroundResource(R.color.white);
        }
        return convertView;
    }

    /**
     * 跳转到课程列表页面
     */
    void goNextActivity(String yxid,String yxmc) {
        Intent i = new Intent(context, CourseListActivity.class);
        i.putExtra("yxid", yxid);
        i.putExtra("yxmc", yxmc);
        context.startActivity(i);
        AnimUtil.intentSlidIn((Activity) context);
    }
}
